package com.bank.service;

import com.bank.dao.TransactionDAO;

import java.sql.SQLException;

public class TransactionService {
    private final TransactionDAO transactionDAO;

    public TransactionService() {
        this.transactionDAO = new TransactionDAO();
    }

    /**
     * 执行转账操作
     * @param fromCard 转出卡号
     * @param toCard 转入卡号
     * @param amount 转账金额
     * @return 转账结果消息
     */
    public String transferFunds(String fromCard, String toCard, double amount) {
        try {// 调用DAO层执行转账
            String[] result = transactionDAO.transferFunds(fromCard, toCard, amount);
            // 解析存储过程返回的结果码
            int resultCode = Integer.parseInt(result[0]);
            String message = result[1];

            switch (resultCode) {
                case 0:
                    return "转账成功！";
                case -2:
                    return "转账失败：余额不足";
                case -3:
                    return "转账失败：转入卡不存在";
                case -4:
                    return "转账失败：转出卡已挂失";
                case -5:
                    return "转账失败：转入卡已挂失";
                case -6:
                    return "转账失败：币种不匹配";
                default:
                    return "转账失败：" + message;
            }
        } catch (SQLException ex) {
            // 异常处理
            ex.printStackTrace();
            return "转账过程中发生错误：" + ex.getMessage();
        }
    }
}
